package com.purdue.edu.psychotron.client.entity;

import java.io.Serializable;
import java.util.ArrayList;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Entity;

import com.purdue.edu.psychotron.shared.UUID;

@Entity
public class ContactBook {

	@Id
	private String userId;
	private ArrayList<String> contactList = new ArrayList<String>();	// List of userId
	private ArrayList<String> blockedContactList = new ArrayList<String>();	// List of userId
	private ArrayList<String> pendingContactList = new ArrayList<String>();	// List of userId
	
	public ArrayList<String> getPendingContactList() {
		return pendingContactList;
	}

	public ContactBook() {
	}

	public static class Builder {
		private ContactBook contactBook = new ContactBook();

		public Builder(ContactBook ContactBook) {
			this.contactBook = ContactBook;
		}

		public Builder(String userId) {
			this.contactBook.userId = userId;
			this.contactBook.contactList = new ArrayList<String>();
			this.contactBook.blockedContactList = new ArrayList<String>();
			this.contactBook.pendingContactList = new ArrayList<String>();

		}

		public Builder initializeContactList(){
			this.contactBook.contactList = new ArrayList<String>();
			this.contactBook.blockedContactList = new ArrayList<String>();
			this.contactBook.pendingContactList = new ArrayList<String>();
			return this;
		}
		
		public void checkIfNullLists(){
			if(this.contactBook.contactList==null){
				this.contactBook.contactList = new ArrayList<String>();
			}
			if(this.contactBook.blockedContactList==null){
				this.contactBook.blockedContactList = new ArrayList<String>();
			}
			if(this.contactBook.pendingContactList==null){
				this.contactBook.pendingContactList = new ArrayList<String>();
			}
			
		}

		public Builder addNewContact(String targetId) {
			checkIfNullLists();
			
			if(this.contactBook.contactList.contains(targetId))		// check if redundent
				return this;
			if(this.contactBook.blockedContactList.contains(targetId))		// Adding one who's blocked will overwrite
				this.contactBook.blockedContactList.remove(targetId);
			
			this.contactBook.contactList.add(targetId);
			return this;
		}
		
		public Builder addPendingContact(String targetId) {
			checkIfNullLists();
			
			if(this.contactBook.pendingContactList.contains(targetId))		// check if redundent
				return this;
			
			this.contactBook.pendingContactList.add(targetId);
			return this;
		}
		
		public Builder acceptContact(String targetId){
			if(this.contactBook.contactList.contains(targetId))		// check if redundent
				return this;
			if(this.contactBook.pendingContactList.contains(targetId))		
				this.contactBook.pendingContactList.remove(targetId);
			
			this.contactBook.contactList.add(targetId);
			return this;
		}
		
		public Builder onContactRejection(String targetId){
			if(this.contactBook.pendingContactList.contains(targetId))		
				this.contactBook.pendingContactList.remove(targetId);
			return this;
		}
		
		
		public Builder onContactAcception(String targetId){
			if(this.contactBook.pendingContactList.contains(targetId))		
				this.contactBook.pendingContactList.remove(targetId);
			if(!this.contactBook.contactList.contains(targetId))		
				this.contactBook.contactList.add(targetId);
			return this;
		}
		
		public Builder rejectContact(String targetId){
			if(this.contactBook.pendingContactList.contains(targetId))		
				this.contactBook.pendingContactList.remove(targetId);
			return this;
		}
		
		public Builder blockContact(String targetId) {
			checkIfNullLists();
			
			if(this.contactBook.contactList.contains(targetId))		// check if it is a current contact
				this.contactBook.contactList.remove(targetId);
			
			if(!this.contactBook.blockedContactList.contains(targetId))
				this.contactBook.blockedContactList.add(targetId);
			return this;
		}
		
		public Builder removeContact(String targetId) {
			checkIfNullLists();
			if(!this.contactBook.contactList.contains(targetId))		// check if it exists
				return this;
			this.contactBook.contactList.remove(targetId);
			return this;
		}

		public ContactBook build() {
			return this.contactBook;
		}
	}


	public ArrayList<String> getContactList() {
		return contactList;
	}
	
	public ArrayList<String> getBlockedContactList() {
		return blockedContactList;
	}
	
	public String getUserId() {
		return userId;
	}

}